from fastapi import APIRouter
import requests
from fastapi.responses import JSONResponse, Response

router = APIRouter(prefix="/news")


def getPdhaUrl(type: str = "json"):
    return f"https://api.suxun.site/api/sixs?type={type}"


def getToken():
    return "lowwiwrgeff6gdjarkbhe6beddz9jm"


@router.get("/zaobao")
async def zaobao(type: str = "json"):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(f"https://v3.alapi.cn/api/zaobao?token={getToken()}&format={type}", headers=headers)
    if type == "image":
        print(response.text)
        return Response(
            content=response.content,
            media_type=response.headers.get("content-type", "image/jpeg")
        )
    else:
        data = response.json()
        if data["code"] == 200:
            return JSONResponse(content=data, status_code=200, media_type="application/json")
        else:
            return JSONResponse(content=data, status_code=400, media_type="application/json")
                